// {
//      width: 宽度, 默认100           
//      label: 表头名字,
//      value: 在object中的数据名,
//      select: {  存在select说明是一个下拉选择框
//          option:  选择框的数据，根据名字在 $store.state.enums取值   
//          selectLabel: select 显示的值 不传则默认为 option 的值
//          selectValue: select 的value值 不传则 默认为 selectLabel 的值
//      },
//      rules: [  校验规则 参考 el-form ，不传则不校验],
//      onchange : (function(data){} || [function,function])  input的onchange  , data参数记录了当前的rows值 传数组会依次执行数组内的方法
//      summary: 当前列是否需要合计 默认false
//      checkSame: "oneCount,oneNoteCount", 传入 a和b两个字段 然后对比这两个值是否相同 相同则标红
//      inputType: 默认未text  数字输入框传入 number , 传入none则直接渲染值且加上disabled样式
//      decimal：数字模式下保留几位小数
// },



// 预设值
let getPresetData = com => {
    return {
        baleNum: {
            label: "包号", value: "baleNum", rules: [
                {
                    required: true,
                    message: "包号不能为空",
                    trigger: "blur",
                },
            ],
        },
        goodsName: {
            width: "110",
            label: "品名",
            value: "goodsName",
            select: "goodsName",
            rules: [
                {
                    required: true,
                    message: "请选择品名",
                    trigger: "blur",
                },
            ],
        },
        brandName: {
            width: "110",
            label: "品牌(批量)",
            value: "brandName",
            select: "productName",
            rules: [
                {
                    required: true,
                    message: "请选择品牌",
                    trigger: "blur",
                },
            ],
        },
        level: {
            width: "110",
            label: "等级(批量)",
            value: "level",
            select: 'levelName',
            rules: [
                {
                    required: true,
                    message: "请选择等级",
                    trigger: "blur",
                },
            ],
        },
        warehouse: {
            width: "120",
            label: "仓库(批量)",
            value: "warehouse",
            select: 'warehouse',
            rules: [
                {
                    required: true,
                    message: "请选择仓库",
                    trigger: "blur",
                },
            ],
        },
        storageLocation: { label: "库位(批量)", value: "storageLocation" },
        ply: {

            label: "厚度(mm)",
            value: "ply",
            inputType: "number",
            onchange: com.checkOneCount,
            rules: [
                {
                    min: 0.01,
                    type: "number",
                    message: "请输入厚度",
                    trigger: "blur",
                },
            ],
        },
        width: {

            label: "宽度(mm)",
            value: "width",
            inputType: "number",
            onchange: com.checkOneCount,
            rules: [
                {
                    min: 0.01,
                    type: "number",
                    message: "请输入宽度",
                    trigger: "blur",
                },
            ],
        },
        length: {
            label: "长度(m)",
            value: "length",
            inputType: "number",
            onchange: com.checkOneCount,
            rules: [
                {
                    min: 0.01,
                    type: "number",
                    message: "请输入长度",
                    trigger: "blur",
                },
            ],
        },
        sliceCount: {
            label: "片数",
            value: "sliceCount",
            inputType: "number",
            onchange: com.checkOneCount,
            rules: [
                {
                    required: true,
                    message: "请输入片数",
                    trigger: "blur",
                },
                {
                    min: 1,
                    type: "number",
                    message: "请输入片数",
                    trigger: "blur",
                },
            ],
        },
        pieceCount: {
            label: "件数",
            value: "pieceCount",
            inputType: "number",
            onchange: [com.inCount, com.noteCount, com.moneyCount],
            summary: true,
            rules: [
                {
                    required: true,
                    message: "请输入件数",
                    trigger: "blur",
                },
                {
                    min: 1,
                    type: "number",
                    message: "请输入件数",
                    trigger: "blur",
                },
            ],
        },
        oneCount: {
            label: "单包方数",
            value: "oneCount",
            inputType: "number",
            decimal: 3,
            onchange: com.inCount,
            checkSame: "oneCount,oneNoteCount",
        },
        inCount: {
            label: "方数",
            value: "inCount",
            inputType: "none",
            decimal: 3,
            checkSame: "inCount,noteCount",
            summary: true,
        },
        oneNoteCount: {
            label: "单包码单",
            value: "oneNoteCount",
            inputType: "number",
            decimal: 3,
            onchange: [com.noteCount, com.moneyCount],
            checkSame: "oneCount,oneNoteCount",
        },
        noteCount: {
            label: "码单",
            value: "noteCount",
            inputType: "none",
            decimal: 3,
            checkSame: "inCount,noteCount",
            summary: true,
        },
        price: {

            label: "单价(批量)",
            value: "price",
            inputType: "number",
            decimal: 3,
            onchange: com.moneyCount,
            rules: [
                {
                    min: 0.01,
                    type: "number",
                    message: "请输入单价",
                    trigger: "blur",
                },
            ],
        },
        money: {
            label: "金额",
            value: "money",
            inputType: "none",
            summary: true,
        },
        caseNum: {
            label: "箱号(批量)", value: "caseNum", rules: [
                {
                    required: true,
                    message: "请输入箱号",
                    trigger: "blur",
                },

            ],
        },
        goodsState: { label: "货物状态(批量)", value: "goodsState", width: "120", },
        originPlace: { label: "产地(批量)", value: "originPlace" },
        sourceOrder: { label: "源单序号", value: "sourceOrder" },
        remark: { label: "备注(批量)", value: "remark", width: 200 },
    }
}


// label和key对应值，
// 这里使用这种方法是不想在 presetData 中用中文做key值
// 虽然没什么影响，纯属多此一举就是了（我就要！！！）
let presetLabelKey = [
    "包号_baleNum",
    "品名_goodsName",
    "品牌_品牌(批量)_brandName",
    "等级_等级(批量)_level",
    "仓库_仓库(批量)_调出仓库_调入仓库_warehouse",
    "库位_库位(批量)_storageLocation",
    "厚度(mm)_ply",
    "宽度(mm)_width",
    "长度(m)_length",
    "片数_sliceCount",
    "件数_pieceCount",
    "方数_调拨方数_inCount",
    "码单_noteCount",
    "单包方数_oneCount",
    "单包码单_oneNoteCount",
    "单价(批量)_price",
    "金额_money",
    "箱号(批量)_caseNum",
    "货物状态(批量)_goodsState",
    "产地(批量)_originPlace",
    "源单序号_sourceOrder",
    "备注(批量)_remark",
].join();

// 根据label值获取预设值
const presetDataByLabel = (label, v, com) => {

    label = label.replace(/\(/g, '\\\(').replace(/\)/g, '\\\)')
    let r = new RegExp(`${label}[^\,]*`);
    let match = presetLabelKey.match(r);

    // 没有就返回false
    if (!match) return v;
    let presetData = getPresetData(com);

    // 根据label去获取字段，再根据字段获取配置项
    let arr = match[0].split('_');
    let k = arr[arr.length - 1];
    let obj = Object.assign({}, presetData[k] || v);

    return Object.assign(obj, v);
}


const getRenderData = (column, com) => {
    let rendetData = [];
    let formData = [{}];
    column.forEach((v, i) => {
        let { label } = v;
        let obj = presetDataByLabel(label, v, com);
        rendetData.push(obj);
        formData[0][obj.value] = '';
    });

    return {
        rendetData,
        formData,
    };
}

export default getRenderData;